import http from '@/public/request'

// 此为去除：字典(dict)和人员基础数据(业务员，操作员，订舱员，客服)，在页面登录之后就已获取到对应的参数,不再调用接口获取数据
const keyHasSym: Array<string> = [
  'custId',
  'tradelaneId',
  'portDepartureId',
  'portDischargeId',
  'portDestinationId',
  'portTransportId',
  'packageUom',
  'vesselid',
  'bookingAgentLinkId',
  'bookingAgentId',
  'shipcompanyId',
  'customsBrokerId',
  'warehousingId'
]

//字段和搜索对应
const keyApiMap = new Map<string, Function>()

//需要下拉数据的对应key
let needKey: string[] = []
//需要下拉数据的对应请求
let needPro: any[] = []
//请求接口 用来和创建标准option
const REQUEST_URL = {
  buenterprise: '/api/buenterprise/dropdownlist',
  dict: '/api/dictDetail/queryfilter',
  port: '/api/base/port/query',
  package: '/api/base/package/query',
  voyage: '/api/base/voyage/query'
}

//委托单位
const wtdw = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/buenterprise/dropdownlist', {
      PageIndex: 1,
      PageSize: 50,
      Type: '客户',
      Id: data
    })
    .then((res) => {
      optionsStore.state.wtdwOption = res.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.entName,
          selectLabel: `${item.entCode}|${item.entFullname}`,
          name: item.entName,
          leftName: item.entCode,
          rigthName: item.entFullname
        }
      })
    })
//运输条款 sys_transterm
const ystk = (data: string, url = REQUEST_URL.dict) =>
  http
    .get('/api/dictDetail/queryfilter', {
      PageIndex: 1,
      PageSize: 50,
      DictName: 'sys_transterm',
      Label: data
    })
    .then((res) => {
      optionsStore.state.ystkOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.value,
          label: item.label,
          selectLabel: item.label
        }
      })
    })
//付款方式 sys_payment
const fkfs = (data: string, url = REQUEST_URL.dict) =>
  http
    .get('/api/dictDetail/queryfilter', {
      PageIndex: 1,
      PageSize: 50,
      DictName: 'sys_payment',
      Label: data
    })
    .then((res) => {
      optionsStore.state.fkfsOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.value,
          label: item.label,
          selectLabel: item.label
        }
      })
    })
//运输方式 sys_transport
const ysfs = (data: string, url = REQUEST_URL.dict) =>
  http
    .get('/api/dictDetail/queryfilter', {
      PageIndex: 1,
      PageSize: 50,
      DictName: 'sys_transport',
      Label: data
    })
    .then((res) => {
      optionsStore.state.ysfsOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.value,
          label: item.label,
          selectLabel: item.label
        }
      })
    })
//业务进度 sys_businessStatus
const ywjd = (data: string, url = REQUEST_URL.dict) =>
  http
    .get('/api/dictDetail/queryfilter', {
      PageIndex: 1,
      PageSize: 50,
      DictName: 'sys_businessStatus',
      Label: data
    })
    .then((res) => {
      optionsStore.state.ywjdOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.value,
          label: item.label,
          selectLabel: item.label
        }
      })
    })
//航线
const hx = (data: string) =>
  http
    .get('/api/base/shipline/query', {
      PageIndex: 1,
      PageSize: 50,
      KeyWords: data
    })
    .then((res) => {
      optionsStore.state.hxOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.shiplineName,
          selectLabel: item.shiplineName
        }
      })
    })
//货物来源 sys_cargoSource
const hwly = (data: string, url = REQUEST_URL.dict) =>
  http
    .get('/api/dictDetail/queryfilter', {
      PageIndex: 1,
      PageSize: 50,
      DictName: 'sys_cargoSource',
      Label: data
    })
    .then((res) => {
      optionsStore.state.hwlyOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.value,
          label: item.label,
          selectLabel: item.label
        }
      })
    })
//提单类型 sys_bl_type
const tdlx = (data: string, url = REQUEST_URL.dict) =>
  http
    .get('/api/dictDetail/queryfilter', {
      PageIndex: 1,
      PageSize: 50,
      DictName: 'sys_bl_type',
      Label: data
    })
    .then((res) => {
      optionsStore.state.tdlxOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.value,
          label: item.label,
          selectLabel: item.label
        }
      })
    })
//装货港
const zhg = (data: string, url = REQUEST_URL.port) =>
  http
    .get('/api/base/port/query', {
      PageIndex: 1,
      PageSize: 50,
      KeyWords: data
    })
    .then((res) => {
      optionsStore.state.zhgOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: `${item.portName} | ${item.portEnName}`,
          selectLabel: `${item.portName} | ${item.portEnName}`
        }
      })
    })
//卸货港
const xhg = (data: string, url = REQUEST_URL.port) =>
  http
    .get('/api/base/port/query', {
      PageIndex: 1,
      PageSize: 50,
      KeyWords: data
    })
    .then((res) => {
      optionsStore.state.xhgOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: `${item.portName} | ${item.portEnName}`,
          selectLabel: `${item.portName} | ${item.portEnName}`
        }
      })
    })
//目的港
const mdg = (data: string, url = REQUEST_URL.port) =>
  http
    .get('/api/base/port/query', {
      PageIndex: 1,
      PageSize: 50,
      KeyWords: data
    })
    .then((res) => {
      optionsStore.state.mdgOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: `${item.portName} | ${item.portEnName}`,
          selectLabel: `${item.portName} | ${item.portEnName}`
        }
      })
    })
//中转港
const zzg = (data: string, url = REQUEST_URL.port) =>
  http
    .get('/api/base/port/query', {
      PageIndex: 1,
      PageSize: 50,
      KeyWords: data
    })
    .then((res) => {
      optionsStore.state.zzgOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: `${item.portName} | ${item.portEnName}`,
          selectLabel: `${item.portName} | ${item.portEnName}`
        }
      })
    })
//包装单位
const bzdw = (data: string, url = REQUEST_URL.package) =>
  http
    .get('/api/base/package/query', {
      PageIndex: 1,
      PageSize: 50,
      KeyWords: data
    })
    .then((res) => {
      optionsStore.state.bzdwOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.packageCode,
          label: item.packageName,
          selectLabel: item.packageName
        }
      })
    })
//业务员
const ywy = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/user/query', {
      PageIndex: 1,
      PageSize: 50,
      Id: data
    })
    .then((res) => {
      optionsStore.state.ywyOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.username,
          selectLabel: item.username
        }
      })
    })
//操作员
const czy = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/user/query', {
      PageIndex: 1,
      PageSize: 50,
      Id: data
    })
    .then((res) => {
      optionsStore.state.czyOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.username,
          selectLabel: item.username
        }
      })
    })
//订舱员
const dcy = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/user/query', {
      PageIndex: 1,
      PageSize: 50,
      Id: data
    })
    .then((res) => {
      optionsStore.state.dcyOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.username,
          selectLabel: item.username
        }
      })
    })
//客服
const kf = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/user/query', {
      PageIndex: 1,
      PageSize: 50,
      Id: data
    })
    .then((res) => {
      optionsStore.state.kfOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.username,
          selectLabel: item.username
        }
      })
    })
//结算方式 sys_settle_type
const jsfs = (data: string, url = REQUEST_URL.dict) =>
  http
    .get('/api/dictDetail/queryfilter', {
      PageIndex: 1,
      PageSize: 50,
      DictName: 'sys_settle_type',
      Label: data
    })
    .then((res) => {
      optionsStore.state.jsfsOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.value,
          label: item.label,
          selectLabel: item.label
        }
      })
    })
//船名航次下的 船名（区分查询表单下的船名（船舶信息））
const cm = (data: string, url = REQUEST_URL.voyage) =>
  http.get('/api/base/voyage/query', {
    PageIndex: 1,
    PageSize: 50,
    KeyWords: data
  })
//联系人
const lxr = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/user/query', {
      PageIndex: 1,
      PageSize: 50,
      Id: data
    })
    .then((res) => {
      optionsStore.state.lxrOption = res.content.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.username,
          selectLabel: item.username
        }
      })
    })
//订舱单位
const dcdw = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/buenterprise/dropdownlist', {
      PageIndex: 1,
      PageSize: 50,
      Type: '订舱单位',
      Id: data
    })
    .then((res) => {
      optionsStore.state.dcdwOption = res.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.entName,
          selectLabel: `${item.entCode}|${item.entFullname}`,
          name: item.entName,
          leftName: item.entCode,
          rigthName: item.entFullname
        }
      })
    })
//船公司
const cgs = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/buenterprise/dropdownlist', {
      PageIndex: 1,
      PageSize: 50,
      Type: '船公司',
      Id: data
    })
    .then((res) => {
      optionsStore.state.cgsOption = res.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.entCode,
          selectLabel: `${item.entCode}|${item.entFullname}`,
          name: item.entName,
          leftName: item.entName,
          rigthName: item.entFullname
        }
      })
    })
//报关行
const bgh = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/buenterprise/dropdownlist', {
      PageIndex: 1,
      PageSize: 50,
      Type: '报关行',
      Id: data
    })
    .then((res) => {
      optionsStore.state.bghOption = res.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.entCode,
          selectLabel: `${item.entCode}|${item.entFullname}`,
          name: item.entName,
          leftName: item.entName,
          rigthName: item.entFullname
        }
      })
    })
//仓库
const ck = (data: string, url = REQUEST_URL.buenterprise) =>
  http
    .get('/api/buenterprise/dropdownlist', {
      PageIndex: 1,
      PageSize: 50,
      Type: '仓库',
      Id: data
    })
    .then((res) => {
      optionsStore.state.ckOption = res.map((item: any) => {
        return {
          id: item.id,
          value: item.id,
          label: item.entCode,
          selectLabel: `${item.entCode}|${item.entFullname}`,
          name: item.entName,
          leftName: item.entName,
          rigthName: item.entFullname
        }
      })
    })

keyApiMap.set('custId', wtdw)
keyApiMap.set('transterm', ystk)
keyApiMap.set('payment', fkfs)
keyApiMap.set('transport', ysfs)
keyApiMap.set('businessStatus', ywjd)
//航线在初始化数据时已处理
// keyApiMap.set('tradelaneId', hx);
keyApiMap.set('cargoSource', hwly)
keyApiMap.set('blType', tdlx)
//港口在初始化数据时已处理
// keyApiMap.set('portDepartureId', zhg);
// keyApiMap.set('portDischargeId', xhg);
// keyApiMap.set('portDestinationId', mdg);
// keyApiMap.set('portTransportId', zzg);
keyApiMap.set('packageUom', bzdw)
keyApiMap.set('salesId', ywy)
keyApiMap.set('operaterId', czy)
keyApiMap.set('bookingId', dcy)
keyApiMap.set('serviceId', kf)
keyApiMap.set('settleType', jsfs)
keyApiMap.set('vesselid', cm)
keyApiMap.set('bookingAgentLinkId', lxr)
keyApiMap.set('bookingAgentId', dcdw)
keyApiMap.set('shipcompanyId', cgs)
keyApiMap.set('customsBrokerId', bgh)
keyApiMap.set('warehousingId', ck)

//分析有数据的key
function getHasDataKey(data: any) {
  for (let i of keyHasSym) {
    if (data[i]) {
      needKey.push(i)
      if (keyApiMap.get(i)) {
        needPro.push(keyApiMap.get(i)!(data[i]))
      }
    }
  }
}

const optionsStore = {
  state: {
    wtdwOption: [],
    ystkOption: [],
    fkfsOption: [],
    ysfsOption: [],
    ywjdOption: [],
    hxOption: [],
    hwlyOption: [],
    tdlxOption: [],
    zhgOption: [],
    xhgOption: [],
    mdgOption: [],
    zzgOption: [],
    bzdwOption: [],
    ywyOption: [],
    czyOption: [],
    dcyOption: [],
    kfOption: [],
    jsfsOption: [],
    cmOption: [],
    lxrOption: [],
    dcdwOption: [],
    cgsOption: [],
    bghOption: [],
    ckOption: [],
    // 箱型箱量
    BoxOption: []
  },
  mutations: {
    /**
     * 获取所有options
     */
    getAllOptions(data: object) {
      needKey = []
      needPro = []
      getHasDataKey(data)

      return Promise.allSettled(needPro)
    }
  }
}

export default optionsStore
